import {
  getNetworkEmpPageSelect,
  getNetworkPageSelect,
  isEmpJoinNetwork as isEmpJoinNetworkAPI,
} from '@/api/network'
import { ref } from 'vue'

/**
 * 获取网点下拉选项
 */
export const useGetNetworkOptions = () => {
  // 获取下拉选项加载
  const loading = ref(false)

  // 获取下拉选项请求参数
  const getOptionsParam = ref({
    limit: 0,
    page: 0,
    name: '',
  })

  // 下拉选项
  const options = ref<Array<NetworkOption>>([])

  /**
   * 获取下拉选项
   */
  const getOptions = async () => {
    loading.value = true
    const res = await getNetworkPageSelect(getOptionsParam.value)
    options.value = res.data.data.rows
    loading.value = false
  }

  return {
    loading,
    options,
    getOptionsParam,
    getOptions,
  }
}

/**
 * 判断工程师是否已经加入了网点中
 */
export const useIsEmpJoinNetwork = () => {
  // 判断加载
  const judgeLoading = ref(false)

  // 工程师是否已经加入了网点
  const isEmpJoinNetwork = ref(false)

  /**
   * 判断方法
   * @param {String} phone - 手机号
   */
  const judge = async (phone: string) => {
    judgeLoading.value = true
    const res = await isEmpJoinNetworkAPI(phone)
    isEmpJoinNetwork.value = res.data.data.row.engineerJoinNetwork
    judgeLoading.value = false
  }

  return {
    judgeLoading,
    isEmpJoinNetwork,
    judge,
  }
}

/**
 * 获取网点人员下拉选项
 */
export const useGetNetworkPersonOptions = () => {
  // 获取下拉选项加载
  const getOptionsLoading = ref(false)

  // 获取下拉选项请求参数
  const getOptionsParam = ref({
    name: '',
    limit: 50,
    page: 1,
  })

  // 下拉选项
  const options = ref<Array<NetworkPersonOption>>([])

  /**
   * 获取下拉选项
   * @param {String} id - 网点id
   */
  const getOptions = async (id: string) => {
    getOptionsLoading.value = true
    const res = await getNetworkEmpPageSelect(getOptionsParam.value, id)
    options.value = res.data.data.rows
    getOptionsLoading.value = false
  }

  return {
    options,
    getOptionsParam,
    getOptionsLoading,
    getOptions,
  }
}
